﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enter.DataCommon
{
	public class MapSetSchema
	{
		IList<string> fields = new List<string>(8);

		IDictionary<string, int> fieldIndexOfs = null;

		public int FieldCount = 0;

		/// <summary>
		/// 增加一个成员字段
		/// </summary>
		/// <param name="exp">成员字段映射的属性（Lambda表达式）</param>
		/// <param name="field">成员字段名称</param>
		/// <param name="isKey">是否作为主键使用</param>
		/// <returns></returns>
		public void AddField(string field)
		{
			field = field.ToLower();
			fields.Add(field);
		}

		public virtual void Compile()
		{
			FieldCount = fields.Count;
			fieldIndexOfs = new Dictionary<string, int>(FieldCount);
			int i = 0;
			foreach (var val in fields)
			{
				fieldIndexOfs.Add(val, i++);
			}
		}

		public virtual int IndexOf(string field)
		{
			field = field.ToLower();
			int val;
			if (fieldIndexOfs.TryGetValue(field, out val))
				return val;
			else
				return -1;
		}
	}
}
